package com.atguigu.eduService.controller;


import com.atguigu.common.Result;
import com.atguigu.eduService.entity.EduChapter;
import com.atguigu.eduService.entity.chapter.Chapter;
import com.atguigu.eduService.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author smday
 * @since 2020-07-27
 */
@RestController
@RequestMapping("/edu/content/chapter")
//@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    /**
     * 树形结构分类列表
     */
    @GetMapping("/{courseId}")
    public Result getTreeByCourseId(@PathVariable("courseId") String courseId) {
        List<Chapter> list = chapterService.getAllChaptersByCourseId(courseId);
        return Result.ok().data("list", list);
    }

    /**
     * 增加章节
     */
    @PostMapping
    public Result add(@RequestBody EduChapter eduChapter) {
        chapterService.save(eduChapter);
        return Result.ok();
    }

    //读取章节
    @GetMapping
    public Result getInfo(@RequestParam("chapterId") String chapterId) {
        EduChapter chapter = chapterService.getById(chapterId);
        return Result.ok().data("chapter", chapter);
    }

    //修改章节
    @PutMapping
    public Result update(@RequestBody EduChapter eduChapter) {
        chapterService.updateById(eduChapter);
        return Result.ok();
    }

    //删除章节
    @DeleteMapping("/{chapterId}")
    public Result delete(@PathVariable("chapterId") String chapterId) {
        return chapterService.deleteChapterById(chapterId) > 0 ?
                Result.ok() : Result.error();
    }



}

